<script type="text/javascript" src="<?=base_url()?>resource/js/layer/layer.js"></script>
<script type="text/javascript" src="<?=base_url()?>resource/js/layer/extend/layer.ext.js"></script>
<script src="<?=base_url()?>resource/js/uploadPreview.min.js" type="text/javascript"></script>
<script src="<?=base_url()?>resource/js/Tdrag.js" type="text/javascript"></script>
<script type="text/javascript">
    /**
     * 显示型号列表
     * @param site
     * @param bid
     * @param tid
     * @param custom_id
     */
    function brandShow(site,tid,bid,custom_id) {
        layer.open({
            type: 1,
            title: '型号列表',
            area: ['700px', '500px'], //宽高
            content: $('#modelList')
        });
        var index = layer.load(2);
        $.post("<?=blang_url('ajax/ajax/GetSapProductModel')?>",{'site':site,'bid':bid,'tid':tid,'custom_id':custom_id},function(data){
            if(data) {
                layer.close(index);
                var obj = JSON.parse(data);
                var custom_id_html='';
                custom_id_html = "<input type='hidden'  name='custom_id' value='" + custom_id + "'>";
                var html = "<form action='<?=blang_url('productClassify/product_sap/AddSiteProductModel')?>' method='post'>" +
                    "<input type='hidden'  name='site' value='" + site + "'>"+
                    "<input type='hidden'  name='bid' value='" + bid + "'>"+
                    custom_id_html+
                    "<input type='hidden'  name='tid' value='" + tid + "'>";
                for (var i = 0; i < obj.length; i++) {
                    html += "<div class='modelName'>" +
                    "<input  name='model[]' type='checkbox' " + obj[i]['ck'] + " value='" + obj[i]['sap_id'] + "_" + obj[i]['name'] + "'>" + obj[i]['name'] + obj[i]['y_n'] +
                    "</div>"
                }
                if(obj.length!=0) {
                    $('#modelData').html(html + "<div style='margin-bottom: 20px;'><input class='btn' onclick='return CkSubmit()' type='submit' value=' 提 交 '></div></form>");
                }else{
                    $('#modelData').html("<div style='margin-top: 200px;margin-left: 250px;'>此品牌没有任何型号</div>");
                }
            }
        });
    }
    /**
     * 显示更新信息
     * @param id
     * @constructor
     */
    function UpdateShow(id){
        var brandLength=$('.moves').length;
        $('#remarkTr').remove();
        $('.remarkClass').remove();
        layer.open({
            type: 1,
            title: '更新品牌信息',
            area: ['500px', '610px'], //宽高
            content: $('#brandInfo')
        });
        new uploadPreview({ UpBtn: "up_file", DivShow: "logo_div", ImgShow: "logo_img" });
        var index = layer.load(2);
        $.post("<?=blang_url('ajax/ajax/GetSiteBrandInfo')?>",{'id':id},function(data){
            if(data) {
                layer.close(index);
                $('#sort_id').empty();
                var productData=JSON.parse(data);
                var obj=productData['productInfo'];
                var remark=productData['productRemark'];
                $('#id').val(obj.id);
                $('#tid').val(obj.tid);
                $('#site').val(obj.site);
                $('#custom_id_val').val(obj.custom_id);
                $('#brandNameTd').html("<span style='font-size: 30px;color: #009ff1'>"+obj.name+"</span>");
                $('#logo_img').attr('src',obj.logo);
                for(var i=1;i<=brandLength;i++){
                    $('#sort_id').append("<option value='"+i+"'>"+i+"</option>");
                }
                $('#sort_id').val(obj.sort_id);
                $('#authorizationVal').val(obj.authorization);
                var ck=[];
                switch (obj.authorization){
                    case '1':
                        ck[1]='checked';
                        break;
                    case '0':
                        ck[0]='checked';
                        break;
                    default :
                        ck[1]='checked';
                        break;
                }
                $('#shangjia').html("<input type='radio' value='1' "+ck[1]+" onclick='RadioExchange()' id='authorization_y' name='authorization'>是"+
                "<input type='radio' value='0' "+ck[0]+" id='authorization_n' onclick='RadioExchange()' name='authorization'>否");
                if(remark[0]['id']){
                    var color='';
                    var typeName='';
                    for(var j=0;j<remark.length;j++){
                        switch (remark[j]['type_name']){
                            case '上架':
                                color='#00CC00';
                                typeName='上';
                                break;
                            case '下架':
                                color='#ff0000';
                                typeName='下';
                                break;
                            default :
                                color='#00CC00';
                                typeName='上';
                                break;
                        }
                        var trHtml="<tr class='remarkClass' style='color: "+color+"'><td width='20%' align='center'>"+remark[j]['add_name']+"("+typeName+")"+"</td><td width='60%'>"+remark[j]['remark']+"</td><td width='20%' align='center'>"+remark[j]['time']+"</td></tr>";
                        $('#remarkTable tr').eq(j).after(trHtml);
                    }
                }
            }
        });
    }
    //显示系列
    function ViewSeries(site,bid){
        $('#seriesName').val('');
        $('#seriesSite').val(site);
        $('#seriesBid').val(bid);
        layer.open({
            type: 1,
            title: '新增系列',
            area: ['520px', '300px'], //宽高
            content: $('#seriesInfo')
        });
        var index = layer.load(2);
        $.post("<?=blang_url('ajax/ajax/GetSiteSeriesList')?>",{'site':site,'bid':bid},function(data){
            if(data) {
                layer.close(index);
                SeriesHtml(data);
            }
        });
    }
    //新增系列
    function AddSeries(){
        var name=$('#seriesName').val();
        var site=$('#seriesSite').val();
        var bid=$('#seriesBid').val();
        var in_array='';
        if(name) {
            $('.modelName').each(function(key,val){
                if(name == $(this).html()){
                    in_array=name;
                }
            });
            if(in_array) {
                alert('<'+in_array+'>已经存在');
                return false;
            }else{
                var index = layer.load(2);
                $.post("<?=blang_url('ajax/ajax/AddSiteProductSeries')?>", {
                    'site': site,
                    'bid': bid,
                    'name': name
                }, function (data) {
                    if(data) {
                        layer.close(index);
                        SeriesHtml(data);
                    }
                });
            }
        }else{
            alert('系列名称不能为空');
            return false;
        }
    }
    //系列HTML
    function SeriesHtml(obj){
        var data=JSON.parse(obj);
        var html='';
        for(var i=0;i<data.length;i++){
            html+="<div class='modelName'>"+data[i]['name']+"</div>";
        }
        $('#seriesList').html(html);
    }
    /**
     * 品牌提交验证
     * @returns {boolean}
     * @constructor
     */
    function CkSubmit(){
        var typeCk=$("input[name='model[]']").is(':checked');
        if(!typeCk){
            layer.tips('请先选择型号', '#modelData', {
                tips: [1, '#0FA6D8'] //还可配置颜色
            });
            return false;
        }
    }

    /**
     * 提交至TR验证
     * @returns {boolean}
     * @constructor
     */
    function PostTR(){
        var idsCk=$("input[name='ids[]']").is(':checked');
        var ids=new Array();
        if(!idsCk){
            layer.tips('请先选择一条数据', '#dataListTable', {
                tips: [1, '#0FA6D8'] //还可配置颜色
            });
            return false;
        }else{
            $("input[name='ids[]']:checked").each(function(){
                var val=$(this).val();
                ids.push(val);
            });
            var index=layer.load(2);
            $.post("<?=blang_url('ajax/ajax/PostBrandTomRepair')?>",{'ids':ids},function(data){
                if(data){
                    layer.close(index);
                    var str=data.split('_');
                    layer.msg(str[0],{icon: str[1]});
                }
            });
        }
    }
    function BrandSort(){
        layer.open({
            type: 1,
            title: '品牌排序',
            area: ['715px', '550px'], //宽高
            content: $('#box')
        });
        $(".son").Tdrag({
            scope:".boxList",
            pos:true,
            dragChange:true,
            changeMode:"sort",
            animation_options:{//运动时的参数
                duration:200,//每次运动的时间
                easing:"linear"//移动时的特效，ease-out、ease-in、linear
            },
            cbEnd:function(){
               // alert(666);
            }
        });
    }
    //全选/全不选
    function CheckAll(){
        var checkAll=$('#checkAll').is(':checked');
        if(checkAll){
            $("input[name='ids[]']").each(function(){
                var bool=$(this).prop('disabled');
                if(bool==false){
                    $(this).prop("checked",true);
                }
            });
        }else{
            $("input[name='ids[]']").each(function(){
                var bool=$(this).prop('disabled');
                if(bool==false){
                    $(this).prop("checked",false);
                }
            });
        }
    }
    /**
     * 上下架交换
     * @constructor
     */
    function RadioExchange(){
        var radioValue  = $('input[name="authorization"]:checked').val();
        var authorizationVal=$('#authorizationVal').val();
        if(radioValue != authorizationVal){
            var upDown='下架';
            if(authorizationVal==0){
                upDown='上架';
            }
            $('#type_name').val(upDown);
            var trHtml="<tr id='remarkTr'><th>"+upDown+"备注：</th><td><input type='text' id='remark' size='40' name='remark'></td></tr>";
            $('#typeTable tr').eq(3).after(trHtml);
        }else{
            $('#remarkTr').remove();
        }
    }
    function sendInfo(){
        var radioValue  = $('input[name="authorization"]:checked').val();
        var authorizationVal=$('#authorizationVal').val();
        if(radioValue != authorizationVal) {
            var remarkTr= $.trim($('#remark').val());
            if(remarkTr==''){
                alert('上下架备注不能为空');
                return false;
            }
        }
    }
</script>
<style>
    .modelName{
        background-image: url("http://oms.tomrepair.es/resource/images/m17_u3.png");
        width: 150px;
        height: 30px;
        line-height:30px;
        overflow:hidden;
        text-align: center;
        float: left;
        margin-right: 15px;
        margin-bottom: 10px;
    }
    .boxList{
        position: relative;
        width:675px;
        height:<?=((77+15)*$length)+15?>px;
        margin:10px auto;
        background:#eee;
        border:1px solid #b8b8b8;
        overflow:hidden
    }
    .son{
        background: #fff;
        width:<?=275/2?>px;
        height:<?=130/2?>px;
        float:left;
        padding:6px;
        margin:15px 0 0 15px;
        cursor: move;
    }
    .tezml{
        padding:5px;
        border:2px dashed #f30;
        opacity:0.5;
        filter:alpha(opacity=50);
    }
    .box{
        margin: 50px;
        padding: 30px;
        background: #eeeeee;
    }
</style>

<!--菜单 start-->
<?php $this->load->view('defaults/common/left');?>
<!--菜单 end-->
<div id="content">
    <div class="page-header">
        <div class="container-fluid">
          <h1>产品分类管理</h1>
          <?=$this->breadcrumb->generateHTML();?>
          <div class="pull-right">
            <input class="btn btn-oms" type="button" onclick="BrandSort()" value="品牌排序">
            <input type="hidden" name="type" value="brand">
            <input type="hidden" name="site" value="<?=$siteId?>">
            <?php if($siteISO!='OMS'){?><input class="btn btn-oms" type="submit" onclick="return PostTR()" value="推送品牌信息至<?=$siteISO?>站点"><?php }?>
             <a href="<?=blang_url('productClassify/product_sap/SiteTypeList?site='.$siteId)?>" title="Back" class="btn btn-default"><i class="fa fa-reply"></i></a>
        </div>
        </div>
    </div>
    <div class="container-fluid">
        <div class="panel panel-default">
          <div class="panel-heading">
            <h3 class="panel-title"><i class="fa fa-list"></i>品牌列表：<span style='color: #009ff1'><?=$name?></span> <span style="color: #ff0000">(<?=$siteISO?>)</h3>
          </div>
          <div class="panel-body">
            <div class="table-responsive">
                <table class="table table-bordered table-hover oms-table" id="dataListTable">
                  <thead>
                    <tr>
                        <th class="text-left"><input type="checkbox" <?=$disabled?> id="checkAll" onclick="CheckAll()">id</th>
                        <th class="text-left">中文名称</th>
                        <th class="text-left">logo</th>
                        <th class="text-right">序号</th>
                        <th class="text-right">是否上架</th>
                        <th class="text-right">子项管理</th>
                        <th class="text-right">操作</th>
                    </tr>
                  </thead>
                  <tbody>
                    <?php if($brandList): foreach ($brandList as $key=>$row):?>
                    <tr>
                        <td class="text-left">
                            <input type="checkbox" <?=$row['logo']?'':'disabled'?> <?=$disabled?> name="ids[]" value="<?=$row['id']?>">
                            <?=$row['bid']?>
                        </td>
                        <td class="text-left"><?=$row['name']?></td>
                        <td class="text-left"><img src="<?=$row['logo']?>" width="<?=275/3?>" height="<?=130/3?>"></td>
                        <td class="text-right"><?=$row['sort_id']?></td>
                        <td class="text-right">
                            <?php
                            switch($row['authorization']){
                                case '1':
                                    echo "<span style='color: #00CC00'>yes</span>";
                                    break;
                                case '0':
                                    echo "<span style='color: #ff0000'>no</span>";
                                    break;
                                default:
                                    break;
                            }
                            ?>
                        </td>
                        <td class="text-right">
                            <a class="btn btn-primary btn-sm" href="<?=blang_url('productClassify/product_sap/SiteModelList/?site='.$siteId.'&bid='.$row['bid'].'&name='.$row['name'].'&tid='.$row['tid'].'&custom_id='.$row['custom_id'])?>">
                                型号管理
                            </a>
                        </td>
                        <td class="text-right">
                            <a class="btn btn-primary btn-sm" href="javascript:void (0)" onclick="brandShow(<?=$siteId?>,<?=$row['tid']?>,<?=$row['bid']?>,'<?=$row['custom_id']?>')">新增型号 </a>
                            <a class="btn btn-oms btn-sm" href="javascript:void (0)" onclick="ViewSeries(<?=$siteId?>,<?=$row['bid']?>)">新增系列</a>
                            <a class="btn btn-primary btn-sm" href="javascript:void (0)" onclick="UpdateShow(<?=$row['id']?>)">更新&logo&排序</a>
                        </td>
                    </tr>

                    <?php endforeach; else:?>
                        <tr><td colspan="7" class="emptyNumber"><?=lang('public_mygdsj')?>....</td></tr>
                    <?php endif; ?>
                </tbody>
                </table>
                <?php if($pageshow):?>
                <div class="page">
                    <?=$pageshow?>
                </div>
                <?php endif;?>
            </div>
          </div>
        </div>
    </div>

    <div style="display: none" id="modelList">
        <div style="margin-top: 10px;margin-left: 20px;" id="modelData">
        </div>
    </div>
    <div style="display: none" id="brandInfo">
        <div class="ct_content" style="margin-top: 10px;margin-left: 20px;" id="infoData">
            <form action="<?=blang_url('productClassify/product_sap/UpdateBrandInfo')?>" method="post" enctype="multipart/form-data">
                <table cellspacing="0" style="border-collapse: collapse;width: 460px;" id="typeTable">
                    <tr>
                        <th>品牌：</th>
                        <td id="brandNameTd">
                        </td>
                    </tr>
                    <tr>
                        <th>
                            LOGO:
                        </th>
                        <td>
                            <div id="logo_div"><img id="logo_img" width="275" height="130" /></div>
                            <input type="file" id="up_file" name="logo" />
                            <input type="hidden" id="id" name="id">
                            <input type="hidden" id="tid" name="tid">
                            <input type="hidden" id="site" name="site">
                            <input type="hidden" id="custom_id_val" name="custom_id">
                            <input type="hidden" id="type_name" name="type_name">
                            <input type="hidden" id="authorizationVal">
                        </td>
                    </tr>
                    <tr>
                        <th>
                            序号 :
                        </th>
                        <td>
                            <select id="sort_id" name="sort_id" style="width: 100px;">
                            </select>
                        </td>
                    </tr>
                    <tr>
                        <th>
                            是否上架 :
                        </th>
                        <td id="shangjia">
                        </td>
                    </tr>
                    <tr align="center">
                        <td colspan="2">
                            <input type="submit" onclick="return sendInfo()" value=" 提 交 " >
                        </td>
                    </tr>
                </table>
            </form>
            <div style="height: 150px;overflow :auto;margin-top: 10px;">
                <table cellspacing="0" style="border-collapse: collapse;width: 460px;" id="remarkTable">
                    <tr>
                        <th width="20%">操作人</th>
                        <th width="60%">备注</th>
                        <th width="20%">操作时间</th>
                    </tr>
                </table>
            </div>
        </div>
    </div>
    <div style="display: none;margin-top: 20px;margin-left: 20px;" id="seriesInfo">
        <input type="hidden" id="seriesSite" name="site">
        <input type="hidden" id="seriesBid" name="bid">
        <span style="color: #1aa8f4">系列名：</span><input type="text" name="name" id="seriesName" size="45">
        <input type="submit" onclick="return AddSeries()" value=" 提 交 ">
        <br><br>
        <span style="color:#ff0000">当前系列：</span>
        <br><br>
        <div id="seriesList" style="width: 500px;"></div>
    </div>
    <div class="boxList" style="display: none;" id='box'>
        <?php foreach($brandList as $res){?>
        <div class="son" id="<?=$res['id']?>">
            <img src="<?=$res['logo']?>" title="<?=$res['name']?>" style="width:<?=275/2?>px; height:<?=130/2?>px;">
        </div>
        <?php }?>
    </div>
</div>